	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_COST_DM_PRICE.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_COST_DM,$login['staff_auth']);

	$docsv_import = FALSE;
	
	$csvf = Class_FILE_CSV::get_upload_file();
	$year4   =  $_POST['year4'];
	
	$hvalues = array();
	$hvalues["start_time"] = $rows['ndate'];
	$end_time = date("Y-m-d H:i:s",time());
	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		$retHead = null;
		$rowall = Class_FILE_CSV::fget_rows($csvf,TRUE,$retHead );
	
		
		if  (!empty($rowall)) {
		$col_heads = array(
			''          ,      //年度'
			'num_from'       , //通数範囲（下限）	整数
			'num_to'          //通数範囲（上限）	整数
		);
	
	
		foreach ($col_heads as $k => $v) {
			if (!empty($v)) {
				$csvheads[$k]  = $v;
				$col_index[$v] = $k;
			}
		}
		
		$types = Class_M_OTHER::get_dmtype_array();
		$typeindex = array();
		foreach ($types as $k => $v) {
			if (!empty($v)) {
				$typeindex[$k]  = array_search($v, $retHead );
			}
		} 
		
		$key1 = $col_index['num_from'];//キーです、必ず仕様します
		
		$err = new Class_ERROR("","");
		$dao = new Class_M_COST_DM_PRICE;
		
		$ngline      = 0;
		$error_times = 0;
		$error_stop  = IMPORT_STOP_MAX_ERROR_TIMES;
		$errtext     = array();
		$isng        = FALSE;
		//$chk         = $dao->get_checks();
		$sort_dm_values = array(); 
		foreach ( $aoki_dm_sort as $kk => $vv) {
			$sort_dm_values[$vv] = $kk;
		}
			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				//CSV内部重複不可チェック
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
						if ($row[$key1] == $vv[$key1]){
							$col = $key1 + 1;
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の{$col}列目の値重複不可。";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				} 
				
				//一般チェック
				$msg =  $err->check( $rowall[$line][$key1], array( "NUM" ) ); 
				if (!empty($msg)) {
					$isng = TRUE;
					$col  = $key1+1;
					$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
					$error_times++;					
					if ($error_times > $error_stop) {
						break;
					}
				}
				
				foreach ($typeindex as $tyk => $tyv) {
					$msg =  $err->check( $rowall[$line][$tyv], array( "NUM" ) ); 
					if (!empty($msg)) {
						$isng = TRUE;
						$col  = $tyv+1;
						$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
						$error_times++;					
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
				

				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}
			
			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
				
				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );	
				}
			} else {
				//DBへ更新
				
				try {
					$daodb = $dao->get_db();
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";
					$executeflg = true; 
					$where = sprintf(" year4 = %s ", $year4  );
					$executeflg = $dao->del($where);
					foreach ( $rowall as $k=>$v ) {
						$i++;
						$percent = intval($i/$line * 100)."%";
						if  ( $percent != $percentold ) {
							Class_FILE_CSV::write_msg_doing( $percent ) ;
						}
						$percentold = $percent;
						$values  = array();
						
						//foreach ( $csvheads as $kk => $vv ) {
						//	$values[$vv] = $v[$kk];
						//}

						$values["num_from"] =  empty($v[$key1]) ? 0 : $v[$key1] ;
						$values["num_to"] =  0;
						$values["year4"]    =  $year4;
		
						if ($id = $dao->add($values)) {
						
						} else {
							$executeflg = false; 
						}
						$subvalues = array();
						if  ($executeflg) {
							foreach ($typeindex as $tyk => $tyv) {
								$subvalues[$tyk] = empty($v[$tyv]) ? 0 :$v[$tyv] ;
							}
	
							if ($dao->delInsertSub( $id , $subvalues )) {
								
							} else {
								$executeflg = false; 
							}
						}
						
						/*
						if ( $dao->exits( $where ) ) {
							if (!($dao->editbywhere($where ,$values ))) {
								throw  new Exception("mcost_dm_price_importdb更新");
							}
							
						} else {
							if ( !( $dao->add($values ) ) ) {
								throw  new Exception("mcost_dm_price_import登録");
							}
						}
						*/
					}
					
					if (!$dao->do_num_to($year4, 999999999)){
						$executeflg = false; 
					}
					
					if ($executeflg) {
						$dao->commit_trans();
						Class_FILE_CSV::write_msg_succed($line, "mcost_dm_price");
						
					} else {
						$dao->rollback_trans();
						Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
					}
					
				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
				}
				
			} 
		}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
			
		}
		
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	
	}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
	